#include "StdAfx.h"
#include "TreeCtrlItem.h"
#include <vector>
#include "GTDTreeCtrl.h"

CTreeCtrlItem CTreeCtrlItem::GetRoot() const
{
	return CTreeCtrlItem(m_ctrl,m_ctrl->GetRootItem());
}

CTreeCtrlItem CTreeCtrlItem::GetRoot( CGTDTreeCtrl& tree )
{
	return CTreeCtrlItem(&tree,tree.GetRootItem());
}

void CTreeCtrlItem::RemoveAllChild()
{
	std::vector<HTREEITEM> vToDel;
	HTREEITEM hChild = m_ctrl->GetChildItem(m_hItem);
	while(hChild)
	{
		hChild = m_ctrl->GetNextSiblingItem(hChild);
	}
	for(size_t i=0;i<vToDel.size();++i)
	{
		m_ctrl->DeleteItem(vToDel[i]);
	}
}

CTreeCtrlItem CTreeCtrlItem::GetFirstChild()const
{
	return CTreeCtrlItem(m_ctrl,m_ctrl->GetChildItem(m_hItem));
}

CTreeCtrlItem CTreeCtrlItem::GetNextSibling()const
{
	return CTreeCtrlItem(m_ctrl,m_ctrl->GetNextSiblingItem(m_hItem));
}

void CTreeCtrlItem::Delete()
{
	m_ctrl->DeleteItem(m_hItem);
	m_hItem = NULL;
}

void CTreeCtrlItem::SetImage( int nImage, int nSelImage )
{
	m_ctrl->SetItemImage(m_hItem,nImage,nSelImage);
}

CString CTreeCtrlItem::GetText() const
{
	return	m_ctrl->GetItemText(m_hItem);	
}

void CTreeCtrlItem::SetText( const CString& text )
{
	m_ctrl->SetItemText(m_hItem,(LPCTSTR)text);
}


CTreeCtrlItem CTreeCtrlItem::AddChild( const CString& strNode,HTREEITEM HAfter )
{	
	HTREEITEM hChild =  m_ctrl->InsertItem(strNode.GetString(),m_hItem,HAfter);
	return CTreeCtrlItem(m_ctrl,hChild);
}

void CTreeCtrlItem::Expand()
{
	m_ctrl->Expand(m_hItem,TVE_EXPAND);
}

CTreeCtrlItem CTreeCtrlItem::GetParent()const
{
	return CTreeCtrlItem(m_ctrl,m_ctrl->GetParentItem(m_hItem));
}

CTreeCtrlItem CTreeCtrlItem::GetPreOrderNext() const
{
	if(CTreeCtrlItem firstChild =  GetFirstChild() )
		return firstChild;
	if(CTreeCtrlItem nextsib = GetNextSibling())
		return nextsib;
	
	CTreeCtrlItem p = GetParent();
	while(p)
	{
		if(CTreeCtrlItem pSib = p.GetNextSibling())
			return pSib;	
		p = p.GetParent();
	}
	return CTreeCtrlItem();
}

CTreeCtrlItem CTreeCtrlItem::GetPreOrderEnd() const
{
	CTreeCtrlItem p = GetParent();
	while(p)
	{
		if(CTreeCtrlItem pSib = p.GetNextSibling())
			return pSib;	
		p = p.GetParent();
	}
	return CTreeCtrlItem();
}

CTreeCtrlItem CTreeCtrlItem::findItemWithData( DWORD_PTR d ) const /*find the item with the data recursively */
{
	CTreeCtrlItem end = GetPreOrderEnd();
	CTreeCtrlItem next  = *this;
	while(next!=end)
	{
		if(next.GetItemData() == d)
			return next;
		next = next.GetPreOrderNext();
	}
	return CTreeCtrlItem();
}

DWORD_PTR CTreeCtrlItem::GetItemData() const
{
	return m_ctrl->GetItemData(m_hItem);
}

TreeItemImpl* CTreeCtrlItem::GetImpl() const
{
	return m_ctrl->GetItemImpl(m_hItem);
}

void CTreeCtrlItem::SetImpl( TreeItemImpl * pImpl )
{
	m_ctrl->SetItemImpl(m_hItem,pImpl);
}

void CTreeCtrlItem::SetState(int iState)
{
	m_ctrl->SetItemState(m_hItem,INDEXTOSTATEIMAGEMASK(iState),TVIS_STATEIMAGEMASK);
}

void CTreeCtrlItem::SetExpandImage( int nImage )
{
	m_ctrl->SetItemExpandedImageIndex(m_hItem,nImage);

}
